/* _____________________________________________________________________________

	Project: The Effects of Independent Local Radio on Tanzanian Public Opinion: Evidence from a Planned Natural Experiment
	Authors: Donald P. Green, Dylan Groves, Constantine Manda, Beatrice Montano, Bardia Rahmani
	
	Purpose: Table A2 ingredients - Village level balance
	Date: 2022.10
________________________________________________________________________________*/


	
/* Introduction ________________________________________________________________*/
	
	clear all	
	set more off
	
	
/* Set Control Directory _______________________________________________________*/

	*global user "X:/Dropbox/Wellspring Tanzania Papers/Wellspring Tanzania - Natural Experiment/pfm_ne_replication"
		
/* Load Data ___________________________________________________________________*/	
	
	use "${user}/pfm_ne_replication_data.dta", clear
	sort id_resp_uid
	set seed 1956
	
/* Reduce to village level dataset _____________________________________________*/	
	
	keep treat id_village_uid id_village_c v_* block_ne
	duplicates drop id_village_uid, force
	
/* Set Vars ____________________________________________________________________*/

	local base_vars v_pop v_timetotown v_mosques v_churches v_cell_bar v_electricity	
	
/* Define Matrix _______________________________________________________________*/
		
	/* Set Put Excel File Name */
	putexcel clear
	putexcel set "${user}/pfm_ne_replication_analysis_balance_village_rawoutput.xlsx", replace 
	
	putexcel A1 = ("variable")
	putexcel B1 = ("variablelabel")
	putexcel C1 = ("treatmean")
	putexcel D1 = ("treatsd")
	putexcel E1 = ("controlmean")
	putexcel F1 = ("controlsd")
	putexcel G1 = ("coef")
	putexcel H1 = ("pval")
	putexcel I1 = ("N")
	putexcel J1 = ("min")
	putexcel K1 = ("max")
	putexcel L1 = ("samplemean")

	
/* Export Analysis ___________________________________________________________*/

	/* Set locals */
	local i = 1
	local row = 2

	/* Run and save for each variable */
	foreach dv of local base_vars  {

		/* Variable Name */
		qui ds `dv'
			global varname = "`r(varlist)'"  
			
		/* Variable Label */
		global varlabel : var label `dv'
			
		/* Treatment / Control Mean */
		sum `dv' if treat == 0 
			global control_mean `r(mean)'
			global control_sd `r(sd)'

		sum `dv' if treat == 1 
			global treat_mean `r(mean)'
			global treat_sd `r(sd)'
			
		sum `dv'
			global sample_mean `r(mean)'

		/* Variable Range */	
		sum `dv' 
			global min = r(min)
			global max = r(max)

		/* Regression  */
		xi: reg `dv' treat i.block_ne, cluster(id_village_uid)						
			matrix table = r(table)
				
			/* Save values from regression */
			global coef = table[1,1]    	//beta
			global pval = table[4,1]		//pval
			global r2 	= `e(r2_a)' 		//r-squared
			global n 	= e(N) 				//N
			
			/* Put excel */
			putexcel A`row' = ("${varname}")
			putexcel B`row' = ("${varlabel}")
			putexcel C`row' = ("${treat_mean}")
			putexcel D`row' = ("${treat_sd}")
			putexcel E`row' = ("${control_mean}")
			putexcel F`row' = ("${control_sd}")
			putexcel G`row' = ("${coef}")
			putexcel H`row' = ("${pval}")
			putexcel I`row' = ("${n}")
			putexcel J`row' = ("${min}")
			putexcel K`row' = ("${max}")
			putexcel L`row' = ("${sample_mean}")

	/* Update locals */
	local row = `row' + 1
	local i = `i' + 1 
}	



	

















